package com.viajero.movil;

import android.app.Activity;
import java.util.List;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class PruebaUbicacionActivity extends Activity implements LocationListener {

	LocationManager mgr;
	TextView output;
	String best;
	
	private static final String[] A={"invalid","n/a","fine","coarse"};
	private static final String[] P={"invalid","n/a","low","medium","high"};
	private static final String[] S={"out of service","temporarily unavailable","available" };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.prueba_ubicacion);
		
		output = (TextView)findViewById(R.id.output);
		
		mgr = (LocationManager)getSystemService(LOCATION_SERVICE);
		
		log("Location provider: ");
		dumpProviders();
		
		Criteria criteria = new Criteria();
		best = mgr.getBestProvider(criteria, true);
		log("\nBest Provider is: "+best);
		
		log("\nLocations (starting with last known): ");
		Location location = mgr.getLastKnownLocation(best);
		dumpLocation(location);
		
		
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mgr.removeUpdates(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mgr.requestLocationUpdates(best,15000, 1,this);
	}




	private void dumpLocation(Location location) {
		// TODO Auto-generated method stub
		if(location == null){
			log("\nLocation[unknown]");
		}else{
			log("\n"+location.toString());
		}
	}


	private void dumpProviders() {
		// TODO Auto-generated method stub
		List<String> providers = mgr.getAllProviders();
		for(String provider : providers){
			dumpProvider(provider);
		}
	}


	private void dumpProvider(String provider) {
		// TODO Auto-generated method stub
		LocationProvider info = mgr.getProvider(provider);
		StringBuilder builder = new StringBuilder();
		builder.append("LocationProvider[")
		.append("name=")
		.append(info.getName())
		.append(", enabled= ")
		.append(mgr.isProviderEnabled(provider))
		.append(", getAccuracy=")
		.append(A[info.getAccuracy()+1])
		.append(", hasMonetareCost=")
		.append(info.hasMonetaryCost())
		.append(", requiresCell=")
		.append(info.requiresCell())
		.append(", requiresNetwork=")
		.append(info.requiresNetwork())
		.append(", requiresSatellite=")
		.append(info.requiresSatellite())
		.append(", supportsAltitude=")
		.append(info.supportsAltitude())
		.append(", supportsBearing=")
		.append(info.supportsBearing())
		.append(", supportsSpeed=")
		.append(info.supportsSpeed())
		.append("]");
		log(builder.toString());
	}

	private void log(String string) {
		// TODO Auto-generated method stub
		output.append(string+"\n");
	}


	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		dumpLocation(location);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		log("\nProvider disabled: " + provider);
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		log("\nProvider enabled: " + provider);
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		log("\nProvider status changed: " + provider+", status="+S[status]+", extras="+extras);
		
	}

	
	
	
}
